import Data from "../object/Data"
import ObservableArray from "../observer/observable.array"
import ExtObj from "./ExtObj"
export default class ExtObjData extends Data {
  extObjList: ObservableArray<ExtObj>
  extObjMap: Record<string, ExtObj>
  currentExtObj: string
  constructor(data?: Record<string, ExtObj>) {
    super()
    this.name = "extobj"
    this.extObjList = new ObservableArray()
    this.extObjMap = {}
    if (data) {
      const dataList = Object.values(data)
      for (const item of dataList) {
        this.extObjMap[item.id] = item
        this.extObjList.push(item)
      }
    }
  }
  refresh(e) {
    for (const t in this.extObjMap) e[t] && this.extObjMap[t].refresh(e[t])
  }
  get currentExtObjObject() {
    if (this.currentExtObj) return this.getExtObj(this.currentExtObj)
    return null
  }
  makeExtObjChangeSubscription(e) {
    return this.onPropertyChanged("currentExtObj", e)
  }
  getExtObj(e: string) {
    return this.extObjMap[e]
  }
  getExtObjById(e: string) {
    return this.extObjList.find(t => t.id === e)
  }
  containsExtObj(e: string) {
    return e in this.extObjMap
  }

  isExtObjDisabled(e = this.currentExtObj) {
    if (!e) return !1
    const t = this.getExtObj(e)
    return t && !t.enable
  }
  *extobjs() {
    for (const e of this.extObjList) yield e
  }
  iterate(e: (v: ExtObj, i?: number) => void) {
    this.extObjList.forEach((t, i) => {
      e(t, i)
    })
  }
  filter(e: (value: ExtObj, index: number, array: ExtObj[]) => boolean) {
    return this.extObjList.filter(e)
  }

  getextObjList() {
    return this.extObjList.values()
  }
}
